﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace Benefit.Service.Sys
{
    /// <summary>
    /// 基金管理员表(需要)
    /// </summary>
    public class ServiceOperator : Benefit.Interface.Sys.IOperator
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperator(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 得到所有操作员
        /// </summary>
        /// <returns></returns>
        public List<Models.Sys.Operator> GetList()
        {
            var query = (from t in db.Operators select t).ToList();
            return query;
        }
        /// <summary>
        /// 得到所有没有没删除的操作员
        /// </summary>
        /// <returns></returns>
        public List<Models.Sys.Operator> GetUnDeletedOperators()
        {
            var query = (from t in db.Operators where t.IsDeleted == false select t).ToList();
            return query;
        }
        /// <summary>
        /// 增加一个交易员
        /// </summary>
        /// <returns>交易员编号</returns>
        public int AddOperator(string email,string password,string phone,string sex,string uname,string username)
        {
            Models.Sys.Operator op = new Models.Sys.Operator();
            Interface.Sys.IBatch _batch = new Sys.ServiceBatch(db);
           // Models.Sys.Batch _batch = new Models.Sys.Batch();
            op.BatchId = _batch.GetNewBatch();
            op.Email = email;
            op.IsDeleted = false;
            op.Password = password;
            op.Phone = phone;
            op.Sex = sex;
            op.UName = uname;
            op.Username = username;
            db.Operators.Add(op);
            db.SaveChanges();

            int operatorid = op.Id;
            Interface.Sys.IOperatorInfo iinfo = new Sys.ServiceOperatorInfo(db);
            //Models.Sys.OperatorInfo info = new Models.Sys.OperatorInfo();
            //info.OperatorId = operatorid;
            iinfo.AddOperatorInfo(operatorid);

            return operatorid;
        }

        public Models.Sys.Operator GetModel(int id)
        {
            Models.Sys.Operator op = null;
            var query = db.Operators.Find(id);
            if (query != null)
            {
                op = query;
            }
            return op;
        }
        /// <summary>
        /// 获取成长指数
        /// </summary>
        /// <param name="lastMonthProfit"></param>
        /// <param name="MonthProfit"></param>
        /// <returns></returns>
        public int GetGrowthIndex(double lastMonthProfit, double MonthProfit)
        {
            if (lastMonthProfit <= 0)
            {
                lastMonthProfit = 1;
            }
            if (MonthProfit <= 0)
            {
                MonthProfit = 1;
            }
            int index = Convert.ToInt32((MonthProfit - lastMonthProfit) / lastMonthProfit) * 100;
            return index;
        }
        /// <summary>
        /// 获取稳定指数
        /// </summary>
        /// <param name="lastMonthProfit"></param>
        /// <param name="MonthProfit"></param>
        /// <returns></returns>
        public int GetStableIndex(double lastMonthProfit, double MonthProfit)
        {
            if (lastMonthProfit <= 0)
            {
                lastMonthProfit = 1;
            }
            if (MonthProfit <= 0)
            {
                MonthProfit = 1;
            }
            lastMonthProfit = Math.Sqrt(lastMonthProfit);
            MonthProfit = Math.Sqrt(MonthProfit);
            int index = 0;
            if (lastMonthProfit > MonthProfit)
            {
                index = Convert.ToInt32(MonthProfit / lastMonthProfit) * 100;
            }
            else
            {
                index = Convert.ToInt32(lastMonthProfit / MonthProfit) * 100;
            }
            return index;
        }
        /// <summary>
        /// 获取指数的图片的数量
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int GetPicCount(int index)
        {
            int returnindex = 0;
            if (index < 20)
            {
                returnindex = 1;
            }
            else if (index >= 20 && index < 40)
            {
                returnindex = 2;
            }
            else if (index >= 40 && index < 60)
            {
                returnindex = 3;
            }
            else if (index >= 60 && index < 80)
            {
                returnindex = 4;
            }
            else
            {
                returnindex = 5;
            }
            return returnindex;
        }
        /// <summary>
        /// 操作员登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public int OperatorLogin(string username, string password, int tradeHistoryId, string pdate)
        {
            int operatorid = 0;
            var _query = db.Operators.Where(a => a.Username.Equals(username)).Where(a => a.Password.Equals(password));
            if (_query.Count() > 0)
            {
                operatorid = _query.First().Id;
                Common.Web.Cookie cookie = new Common.Web.Cookie();
                cookie.SetCookie("LoginUserId", operatorid.ToString(), 48);
                cookie.SetCookie("LoginUserType", Convert.ToInt32(Models.Config.UserType.Operator).ToString(), 48);
                cookie.SetCookie("TradingDayId", tradeHistoryId.ToString(), 48);
                cookie.SetCookie("TradingDayPdate", pdate, 48);
            }
            return operatorid;
        }

        /// <summary>
        /// 得到同一个批次的所有交易员
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public List<Models.Sys.Operator> GetBatchOperators(int batch)
        {
            var query = db.Operators.Where(a => a.BatchId == batch).Where(a => a.IsDeleted == false).ToList();
            return query;
        }

        /// <summary>
        /// 判断交易员当日是否有做交易
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public bool OperatorTradedAtDay(int operatorid, int tradeHistoryId)
        {
            bool traded = false;
            Interface.View.IOperatorDayProductInfo iopinfo = new View.ServiceOperatorDayProductInfo(db);
            List<Models.View.OperatorDayProductInfo> operatorinfos = iopinfo.GetOperatorDayProductInfo(tradeHistoryId, operatorid);
            if (operatorinfos.Count > 0)
            {
                traded = true;
                return traded;
            }

            return traded;
        }

        /// <summary>
        /// 判断交易员当日是否有做交易
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public bool OperatorTradedAtMonth(int operatorid, int month, int year)
        {
            bool traded = false;
            Interface.View.IOperatorDayProductInfo iopinfo = new View.ServiceOperatorDayProductInfo(db);
            List<Models.View.OperatorDayProductInfo> operatorinfos = iopinfo.GetOperatorMonthProductInfo(month, year, operatorid);
            if (operatorinfos.Count > 0)
            {
                traded = true;
                return traded;
            }

            return traded;
        }

    }
}